<?php
defined('IN_PHPCMS') or exit('No permission resources.');
// 模型缓存路径
define('CACHE_MODEL_PATH', CACHE_PATH . 'caches_model' . DIRECTORY_SEPARATOR . 'caches_data' . DIRECTORY_SEPARATOR);
// 定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML', true);

pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format', '', 0);

/**
 * 资讯绑定人才
 */
class talent extends admin
{

    private $db,$tsmodel;

    public $siteid;

    public function __construct()
    {
        parent::__construct();
        $this->db = pc_base::load_model('news_nsr_model');
        $this->tsmodel = pc_base::load_model('talent_source_model');
        $this->siteid = get_siteid();
        $model_setting=pc_base::load_model('site_setting_model');
        $this->t_type = $model_setting->getValue("talent_type",$this->siteid);//获取人才类型
        $this->talent_source= $model_setting->getValue("talent_source",$this->siteid);//获取人才来源
        $this->talent_xl = $model_setting->getValue("talent_xl",$this->siteid);//获取人才学历
        $this->continent= $model_setting->getValue("talent_continent",$this->siteid);//获取人才所在州
        $this->nation = $model_setting->getValue("talent_nation",$this->siteid);//获取人才国家
        $city = $model_setting->getValue("city",$this->siteid);//获取城市
        $province = $model_setting->getValue("talent_province",$this->siteid);//获取人才省份
        $new_city = array();
        foreach ($city as $k=>$v){
            $new_city[$v] = $v;
        }
        $this->city = $new_city;
        $company = $model_setting->getValue("company",$this->siteid);//获取人才所属公司
        $new_company = array();
        foreach ($company as $k=>$v){
            $new_company[$v] = $v;
        }
        $this->company= $new_company;
        $talent_province = array();
        foreach ($province as $v){
            $talent_province[$v] = $v;
        }
        $this->province = $talent_province;
    }

    /**
     * 资讯绑定人才列表
     */
    public function index()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $show_dialog = $show_validator = true;
        $show_header = '';
        $id= $_REQUEST['id'];
        $source_type = $_REQUEST['source_type'];
        if ($source_type == 1) {
            $info = $this->db->get_one(array('id' => $id));
        } else if($source_type == 2){
            $video_model = pc_base::load_model('video_nsr_model');
            $info = $video_model->get_one(array('id' => $id));
        } else if($source_type == 3){
            $picture_model = pc_base::load_model('picture_nsr_model');
            $info = $picture_model->get_one(array('id' => $id)); 
        } 
        $types = $this->t_type;

        $sql="select n.*,t.id as cid,t.is_top from nsrweb_talent_source as t LEFT JOIN nsrweb_talent as n on t.talent_id =n.id where n.status=1 and t.source_type = " . $source_type;
        $sql .= " and  t.source_id=".$id." and siteid =" . $this->siteid;
        if (isset($_GET['name']) && !empty($_GET['name'])) {
            $name = trim($_GET['name']);
            $sql .= " and `real_name` like '%$name%' ";
        }
        if (isset($_GET['talent_type']) && !empty($_GET['talent_type'])) {
            switch ($_GET['talent_type']) {
                case '1':
                    $sql .= " and is_student = 1";
                    break;
                case '2':
                    $sql .= " and is_signed_model = 1";
                    break;
                case '3':
                    $sql .= " and is_hot = 1";
                    break;
                case '4':
                    $sql .= " and is_judge = 1";
                    break;
                case '5':
                    $sql .= " and is_guest = 1";
                    break;
                case '6':
                    $sql .= " and is_star = 1";
                    break;
                case '7':
                    $sql .= " and is_mentor = 1";
                    break;
                case '8':
                    $sql .= " and is_moter = 1";
                    break;
                case '9':
                    $sql .= " and is_missworld = 1";
                    break;
                default:
                    break;
            }
        }
        $sql .= " order by t.is_top desc ";
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $contestinfo = $this->db->my_listinfo($sql, $page, 10);

        $pages = $this->db->pages;
        include $this->admin_tpl('talent_list');
    }

    /**
     * 添加人才
     */
    public function add()
    {
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $data = isset($_POST['talent']) ? $_POST['talent'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
            $contest_model = pc_base::load_model('talent_model');
            $result = $contest_model->moter_add($data);
            if ($result) {
                $data1['created_by'] =  $_SESSION['userid'];
                $data1['updated_by'] =  $_SESSION['userid'];

                $data1['create_time'] = date('Y-m-d H:i:s');
                $data1['update_time'] = date('Y-m-d H:i:s');
                if ($_POST['catid']) {
                    $data1['cate_id'] = $_POST['catid'];
                    $data1['source_type'] = 4;
                    $data1['source_id'] = $result;
                    $category_source_model = pc_base::load_model('category_source_model');
                    $log_content = '在栏目id为'.$_POST['catid'].'下，';
                    $res = $category_source_model->insert($data1);
                } else {
                    $data1['talent_id'] = $result;
                    $data1['source_type'] = $_POST['source_type'];
                    $data1['source_id'] = $_POST['id'];
                    $log_content = '在'.($_POST['source_type'] == 1?'资讯':($_POST['source_type'] == 2?'视频':'图集')).'id为'.$_POST['id'].'下，';
                    $res = $this->tsmodel->insert($data1);
                }

                //记录行为日志
                $log_content .= '<span style="color:green;">添加人才</span>[人才id：'.$result.'，真实姓名：'.$data['real_name'].']';
                $this->manage_admin_log($log_content);

                if ($res) {
                    if ($_POST['catid']) {
                        showmessage(L('operation_success'), '?m=admin&c=resources&a=tIndex&catid='.$_POST['catid'],'','add');
                    } else {
                        showmessage(L('operation_success'), '?m=information&c=talent&a=index&source_type='.$_POST['source_type'].'&id='.$_POST['id'],'','add');
                    }
                } else {
                    showmessage(L('operation_failure'), HTTP_REFERER);
                }
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $id= $_REQUEST['id'];
            $catid= $_REQUEST['catid'];
            $source_type = $_REQUEST['source_type'];
            $action_source = 'talent_categroy';
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('moter_add',"moter");
        }
    }

    /**
     * 修改人才
     */
    public function edit()
    {
        $contest_model = pc_base::load_model('talent_model');
        if (!isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        $catid = intval($_GET['catid']);
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $contest_model = pc_base::load_model('talent_model');
            $result = $contest_model->moter_edit($_POST['talent'],'',$id);
            if ($result) {
                //记录行为日志
                $log_content .= '<span style="color:blue">修改人才</span>[人才id：'.$id.'；真实姓名：'.$_POST['talent']['real_name'].']';
                $this->manage_admin_log($log_content);
                showmessage(L('operation_success'), '?m=admin&c=resources&a=tIndex&catid='.$catid,'','edit');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $info = $contest_model->get_one(array('id'=>$id));
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('talent_edit','information');
        }
    }

    /**
     * 添加课程
     */
    public function addcourse()
    {
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $course = isset($_POST['course']) ? $_POST['course'] : showmessage(L('error_return'),HTTP_REFERER);
            $data['agency_id'] = isset($course['agency']) ? $course['agency'] : showmessage(L('please_select_course_agency'),HTTP_REFERER);
            $data['name'] = trim($course['name']);
            $data['object'] = $course['object'];
            $data['target'] = $course['target'];
            $data['content'] = $course['content'];
            $data['image_horizontal'] = $course['image_horizontal'];
            $data['image_vertical'] = $course['image_vertical'];
            $data['image_square'] = $course['image_square'];
            $data['course_day'] = isset($course['day']) ? implode(',',$course['day']) : showmessage(L('please_select_day'),HTTP_REFERER);
            $data['course_time'] = $course['time'];
            $data['course_period'] = $course['period'];
            $data['price'] = trim($course['price']);
            $data['special'] = $course['special'];
            $data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
            $data['created_by'] = $data['updated_by'] = $_SESSION['userid'];
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $course_nsr_model = pc_base::load_model('course_nsr_model');
            $result = $course_nsr_model->insert($data,true);
            if ($result) {
                $data1['created_by'] =  $_SESSION['userid'];
                $data1['updated_by'] =  $_SESSION['userid'];

                $data1['create_time'] = date('Y-m-d H:i:s');
                $data1['update_time'] = date('Y-m-d H:i:s');
                $data1['cate_id'] = $_POST['catid'];
                $data1['source_type'] = 6;
                $data1['source_id'] = $result;
                $category_source_model = pc_base::load_model('category_source_model');
                $res = $category_source_model->insert($data1);

                //记录行为日志
                $log_content = '<span style="color:green;">添加课程</span>[课程id：'.$result.'，课程姓名：'.$data['name'].']';
                $this->manage_admin_log($log_content);

                if ($res) {
                    showmessage(L('operation_success'), '?m=admin&c=resources&a=cIndex&catid='.$_POST['catid'],'','add');
                } else {
                    showmessage(L('operation_failure'), HTTP_REFERER);
                }
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $catid= $_REQUEST['catid'];
            $action_source = 'talent_categroy';
            $where = "";
            $where .= "siteid = ".$this->siteid." and status = 1";
            $agency_db = pc_base::load_model('agency_nsr_model');
            $agency = $agency_db->select($where,'id,full_name');//获取机构
            $agency_new= array();
            foreach ($agency as $key=>$value){
                $agency_new[$value['id']] = $value['full_name'];
            }
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('course_nsr_add','course_nsr');
        }
    }

    /**
     * 修改课程
     */
    public function editcourse()
    {
        $course_nsr_model = pc_base::load_model('course_nsr_model');
        if (!isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        $catid = intval($_GET['catid']);
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $course = isset($_POST['course']) ? $_POST['course'] : showmessage(L('error_return'),HTTP_REFERER);
            $data['agency_id'] = isset($course['agency']) ? $course['agency'] : showmessage(L('please_select_course_agency'),HTTP_REFERER);
            $data['name'] = trim($course['name']);
            $data['object'] = $course['object'];
            $data['target'] = $course['target'];
            $data['content'] = $course['content'];
            $data['image_horizontal'] = $course['image_horizontal'];
            $data['image_vertical'] = $course['image_vertical'];
            $data['image_square'] = $course['image_square'];
            $data['course_day'] = isset($course['day']) ? implode(',',$course['day']) : showmessage(L('please_select_day'),HTTP_REFERER);
            $data['course_time'] = $course['time'];
            $data['course_period'] = $course['period'];
            $data['price'] = trim($course['price']);
            $data['special'] = $course['special'];
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $data['updated_by'] = $_SESSION['userid'];
            $data['siteid'] = $this->siteid;
            $result = $course_nsr_model->update($data,array('id'=>$id));
            if ($result) {
                //记录行为日志
                $log_content .= '<span style="color:blue">修改课程</span>[课程id：'.$id.'；课程名称：'.$data['name'].']';
                $this->manage_admin_log($log_content);
                showmessage(L('operation_success'), '?m=admin&c=resources&a=cIndex&catid='.$catid,'','edit');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $course_info = $course_nsr_model->get_one(array('id'=>$id));
            $where = "";
            $where .= "siteid = ".$this->siteid." and status = 1";
            $agency_db = pc_base::load_model('agency_nsr_model');
            $agency = $agency_db->select($where,'id,full_name');//获取机构
            $agency_new= array();
            foreach ($agency as $key=>$value){
                $agency_new[$value['id']] = $value['full_name'];
            }
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('editcourse');
        }
    }

    /**
     * 删除、批量删除
     */
    public function delete()
    {
        if ($_GET['talent']){
            $this->tsmodel = pc_base::load_model('category_source_model');
        }
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        $talent_id = array();
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $info = $this->tsmodel->get_one(array('id'=>$v));
                    $talent_id[] = $info['source_type'] == 4?$info['source_id']:$info['talent_id'];
                    $source_name = $info['source_type'];
                    $source_id = $info['source_type'] == 4?$info['cate_id']:$info['source_id'];
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        } else {
            $info = $this->tsmodel->get_one(array('id'=>$id));
            $talent_id[] = $info['source_type'] == 4?$info['source_id']:$info['talent_id'];
            $source_name = $info['source_type'];
            $source_id = $info['source_type'] == 4?$info['cate_id']:$info['source_id'];
        }
        $result = $this->tsmodel->delete("id in ('$id')");
        if ($source_name == 4) {
            //获取栏目信息
            $category_model = pc_base::load_model('category_model');
            $catinfo = $category_model->get_one(array('catid'=>$source_id));
        }
        //记录行为日志
        $talent_id = implode(',',$talent_id);
        $source_name = $source_name == 1?'资讯':($source_name == 2?'视频':($source_name == 3?'图集':'栏目'));
        $log_content = '<span style="color:red;">解除绑定关系</span>[解除'.$source_name.'id：'.$source_id.($catinfo['catname']?',栏目名称：'.$catinfo['catname']:'').'和人才id：('.$talent_id.')的绑定]';
        $this->manage_admin_log($log_content);

        if ($result) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 排序
     */
    public function listorder() {
        if ($_GET['talent']){
            $this->tsmodel = pc_base::load_model('category_source_model');
        }
        if(isset($_GET['dosubmit'])) {
            foreach($_POST['listorders'] as $id => $listorder) {
                $res = $this->tsmodel->update(array('sort'=>$listorder), array('id'=>$id));
            }
            showmessage(L('operation_success'));
        } else {
            showmessage(L('operation_failure'));
        }
    }

    /**
     * 置顶
     */
    public function isTop ()
    {
        if ($_GET['talent']){
            $this->tsmodel = pc_base::load_model('category_source_model');
        }
        if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id= $_REQUEST['id'];
        $is_top = $_REQUEST['is_top'];
        $res = $this->tsmodel->update(array('is_top'=>$is_top), array('id'=>$id));

        if ($res) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 选择人才绑定
     */
    public function bindTalent(){
        $types = $this->t_type;
        $id= $_REQUEST['id'];
        $catid = $_REQUEST['catid'];
        $source_type = $_REQUEST['source_type'];
        $category_source_model = pc_base::load_model('category_source_model');
        if ($catid) {
            $talent_array = $category_source_model->select(array("cate_id"=>$catid),"source_id");
        } else {
            $talent_array = $this->tsmodel->select(array("source_id"=>$id,"source_type"=>$source_type),"talent_id");
        }
        $where = " status = 1 AND siteid = " . $this->siteid;
        $ids = array();
        if(!empty($talent_array)){
            foreach ($talent_array as $k => $v) {
                $ids[] = intval($v['talent_id']?$v['talent_id']:$v['source_id']);
            }
            $ids = implode('\',\'', $ids);
            $where.=" and id not in ('$ids')";
        }
        if (isset($_GET['real_name']) && !empty($_GET['real_name'])) {
            $name = trim($_GET['real_name']);
            $where .= " and `real_name` like '%$name%' ";
        }
        //是否有模卡
        if (isset($_GET['model_card_img'])) {
            if($_GET['model_card_img']==1){
                $where.=" and model_card_img !=''";
            }
        }
        //是否有模卡组图
        if ($_GET['model_card_imgs']) {
            if($_GET['model_card_imgs']==1){
                $where.=" and model_card_imgs !=''";
            }
        }           
        if (isset($_GET['source']) && $_GET['source']) {
            $where .= " and source = ".$_GET['source'];
        }
        if (isset($_GET['talent_type']) && !empty($_GET['talent_type'])) {
            switch ($_GET['talent_type']) {
                case '1':
                    $where .= " and is_student = 1";
                    break;
                case '2':
                    $where .= " and is_signed_model = 1";
                    break;
                case '3':
                    $where .= " and is_hot = 1";
                    break;
                case '4':
                    $where .= " and is_judge = 1";
                    break;
                case '5':
                    $where .= " and is_guest = 1";
                    break;
                case '6':
                    $where .= " and is_star = 1";
                    break;
                case '7':
                    $where .= " and is_mentor = 1";
                    break;
                case '8':
                    $where .= " and is_moter = 1";
                    break;
                case '9':
                    $where .= " and is_missworld = 1";
                    break;
                default:
                    break;
            }
        }
        $talent_model = pc_base::load_model('talent_model');
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $infos=$talent_model->listinfo($where,'id DESC',$page,10);
        $pages = $talent_model->pages;
        include $this->admin_tpl('bindTalent');
    }

    /**
     * 完成绑定
     */
    public function bindContestAjax(){
        $id = $_REQUEST['id'];
        $catid = $_REQUEST['catid'];
        $source_type = $_REQUEST['source_type'];
        $relation=$_REQUEST['relation'];
        $contest=explode("|",$relation);
        $data=array();
        foreach($contest as $k=>$v){
            if ($catid) {
                //获取栏目信息
                $category_model = pc_base::load_model('category_model');
                $catinfo = $category_model->get_one(array('catid'=>$catid));
                $this->tsmodel = pc_base::load_model('category_source_model');
                $data[$k]['cate_id']=$catid;
                $data[$k]['source_id']=$v;
                $data[$k]['source_type']=4;
                $source_name = '栏目id：'.$catid.'，栏目名称：'.$catinfo['catname'];
                $talent_model = pc_base::load_model('talent_model');
                $info = $talent_model->get_one(array('id'=>$v),'create_time');
                $data[$k]['update_time'] = $info['create_time'];
            } else {
                $data[$k]['talent_id']=$v;
                $data[$k]['source_id']=$id;
                $data[$k]['source_type']=$source_type;
                $source_name = ($source_type==1?'资讯':($source_type==2?'视频':'图集')).'id：'.$id;
                $data[$k]['update_time'] = date('Y-m-d H:i:s');
            }
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['updated_by'] =  $_SESSION['userid'];

            $data[$k]['create_time'] = date('Y-m-d H:i:s');
            
        }
        $res = $this->tsmodel->multiInsert($data);

        //记录行为日志
        $contest_id = implode(',',$contest);
        $log_content = '<span style="color:green;">建立绑定关系</span>['.$source_name.'和人才id：('.$contest_id.')的绑定]';
        $this->manage_admin_log($log_content);

        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }

     /**
     * 选择课程绑定
     */
    public function bindCourse(){
        $types = $this->t_type;
        $catid = $_REQUEST['catid'];
        $category_source_model = pc_base::load_model('category_source_model');
        if ($catid) {
            $talent_array = $category_source_model->select(array("cate_id"=>$catid,'source_type'=>6),"source_id");
        }
        $where = " status = 1 AND siteid = " . $this->siteid;
        $ids = array();
        if(!empty($talent_array)){
            foreach ($talent_array as $k => $v) {
                $ids[] = intval($v['source_id']);
            }
            $ids = implode('\',\'', $ids);
            $where.=" and id not in ('$ids')";
        }
        if (isset($_GET['name']) && !empty($_GET['name'])) {
            $name = trim($_GET['name']);
            $where .= " and `name` like '%$name%' ";
        }
        $course_nsr_model = pc_base::load_model('course_nsr_model');
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        $infos=$course_nsr_model->listinfo($where,'id DESC',$page,10);
        $pages = $course_nsr_model->pages;
        include $this->admin_tpl('bindCourse');
    }

    /**
     * 完成绑定
     */
    public function bindCourseAjax(){
        $catid = $_REQUEST['catid'];
        $relation=$_REQUEST['relation'];
        $contest=explode("|",$relation);
        $data=array();
        foreach($contest as $k=>$v){
            //获取栏目信息
            $category_model = pc_base::load_model('category_model');
            $catinfo = $category_model->get_one(array('catid'=>$catid));
            $this->tsmodel = pc_base::load_model('category_source_model');
            $data[$k]['cate_id']=$catid;
            $data[$k]['source_id']=$v;
            $data[$k]['source_type']=6;
            $source_name = '栏目id：'.$catid.'，栏目名称：'.$catinfo['catname'];
            $course_nsr_model = pc_base::load_model('course_nsr_model');
            $info = $course_nsr_model->get_one(array('id'=>$v),'create_time');
            $data[$k]['update_time'] = $info['create_time'];
            
            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['updated_by'] =  $_SESSION['userid'];

            $data[$k]['create_time'] = date('Y-m-d H:i:s');
            
        }
        $res = $this->tsmodel->multiInsert($data);

        //记录行为日志
        $contest_id = implode(',',$contest);
        $log_content = '<span style="color:green;">建立绑定关系</span>['.$source_name.'和课程id：('.$contest_id.')的绑定]';
        $this->manage_admin_log($log_content);

        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }
}